package com.hs.panghu.dao.imp;

import com.hs.panghu.dao.CommunityDao;
import com.hs.panghu.entity.Community;
import com.hs.panghu.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class CommunityDaoImpl implements CommunityDao {
    @Override
    public int addCommunity(Community community) {
        String sql = "insert into community values(null,?,?,?,?,?,?,?,now(),?,?,?,?,?,?)";
        Object object[] = {
                community.getCommunityName(),
                community.getCommunityProvince(),
                community.getCommunityCity(),
                community.getCommunityDistrict(),
                community.getCommunityAddress(),
                community.getCommunityLatitude(),
                community.getCommunityLongitude(),
                community.getCommunityShopCount(),
                community.getCommunityAreaCode(),
                community.getCommunityCoordinates(),
                community.getCommunityStatus(),
                community.getCommunityUserCount(),
                community.getDistance()
        };
        int row = JDBCDruidUtil.update(sql, object);
        return row;
    }

    @Override
    public int editCommunity(Community community) {
//编写Sql语句
        String str = "update community set communityName = ?,communityProvince = ?" +
                ",communityCity = ?,communityDistrict = ?,communityAddress = ? where communityId = ?";
//将小区对象数据放入数组中
        Object obj[] = {community.getCommunityName(),community.getCommunityProvince(),
                community.getCommunityCity(),community.getCommunityDistrict(),community.getCommunityAddress()
                ,community.getCommunityId()};
        int update = JDBCDruidUtil.update(str, obj);
        return update;
    }

    @Override
    public int delCommunity(Integer communityId) {
        //编写Sql语句
        String str = "delete from community where communityId = ?";
        Object obj[] = {communityId};
        //执行Sql语句
        int row = JDBCDruidUtil.update(str, obj);
        //返回结果
        return row;
    }

    @Override
    public List<Community> queryCommunitys(Map<String, String> map) {
        List<Community> list = new ArrayList<>();
//        动态sql和动态数组
        StringBuffer sql = new StringBuffer("select * from community where 1=1");
        List param = new ArrayList();
//        判断name是否有数据，如果有，则拼接
        String name = map.get("name");
        if(name != null && !"".equals(name)){
            sql.append(" and communityName like ? ");
            param.add("%"+name+"%");
        }
//         判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if(startTime != null && !"".equals(startTime)){
            sql.append(" and createTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
//        分页处理
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        sql.append(" order by communityId desc limit ?,?");
        param.add((pageNum-1)*pageSize);
        param.add(pageSize);

//        封装sql和object 调用工具类
        String sql2 = sql.toString();
        Object object[] = param.toArray();
        ResultSet resultSet = JDBCDruidUtil.query(sql2, object);
        try{
            while (resultSet.next()){
                Community community = new Community(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getString(5),
                        resultSet.getString(6),
                        resultSet.getString(7),
                        resultSet.getString(8),
                        resultSet.getDate(9),
                        resultSet.getInt(10),
                        resultSet.getInt(11),
                        resultSet.getString(12),
                        resultSet.getInt(13),
                        resultSet.getInt(14),
                        resultSet.getString(15)
                );
                list.add(community);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCDruidUtil.close();
        }
        return list;
    }

    @Override
    public int queryCounts(Map<String, String> map) {
        int count = 0;
        StringBuffer sql = new StringBuffer("select count(1) from community where 1=1");
        List param = new ArrayList();
//        判断name是否有数据，如果有，则拼接
        String name = map.get("name");
        if(name != null && !"".equals(name)){
            sql.append(" and communityName like ? ");
            param.add("%"+name+"%");
        }
//         判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if(startTime != null && !"".equals(startTime)){
            sql.append(" and createTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }


//        封装sql和object 调用工具类
        String sql2 = sql.toString();
        Object object[] = param.toArray();
        ResultSet resultSet = JDBCDruidUtil.query(sql2, object);
        try{
           if(resultSet.next()){
               count = resultSet.getInt(1);
           }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCDruidUtil.close();
        }
        return count;
    }
}
